import 'package:flutter/material.dart';
import 'package:flutter_wan_android/arch/ui/toast.dart';
import 'package:flutter_wan_android/arch/util/sputil.dart';
import 'package:flutter_wan_android/res/colors.dart';
import 'package:provider/provider.dart';

class WTheme with ChangeNotifier {
  static WTheme _instance = WTheme._internal();

  factory WTheme() => _instance;

  ///通用全局单例，第一次使用时初始化
  WTheme._internal() {
    init();
  }

  static String isDark = "isDark";

  static ColorModel light = ColorModel(
      background: WColor.bg_light,
      bottomBar: WColor.white2,
      listItemBg: WColor.white,
      text1: WColor.black1,
      txItemBg: WColor.gray3,
      mineHead: WColor.blue1,
      primary: WColor.blue1
  );

  static ColorModel black = ColorModel(
      background: WColor.bg_dark,
      bottomBar: WColor.black1,
      listItemBg: WColor.black4,
      text1: WColor.white3,
      txItemBg: WColor.black1,
      mineHead: WColor.black4,
      primary: WColor.black1
  );

  ColorModel current = light;

  switchTheme() {
    SpUtil.getBool(isDark).then((value) {
      current = value ? light : black;
      SpUtil.put(isDark, current == black);
      notifyListeners();
    });
  }

  init() {
    SpUtil.getBool(isDark).then((value) {
      current = value ? black : light;
      notifyListeners();
    });
  }
}

class ColorModel {
  Color? background;
  Color? bottomBar;
  Color? listItemBg;
  Color? text1;
  Color? txItemBg;
  Color? primary;
  Color? mineHead;

  ColorModel({
    this.background,
    this.bottomBar,
    this.listItemBg,
    this.text1,
    this.txItemBg,
    this.primary,
    this.mineHead
  });
}

extension whteme_provider_context on BuildContext {
  /// 扩展方法
  // ThemeModel wTheme(){
  //   return this.watch<WTheme>().current;
  // }

  /// 扩展属性
  ColorModel get wTheme =>
      this
          .watch<WTheme>()
          .current;
}

extension whteme_provider_state on State {
  /// 扩展属性
  ColorModel get wTheme =>
      this.context
          .watch<WTheme>()
          .current;
}
